//
//  WePersonalController.m
//  WeGalaxy
//
//  Created by Zheng on 2017/7/6.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WePersonalController.h"

#import "WePersonalHeaderInfoView.h"
#import "WePersonalTableHeaderView.h"
#import "WePersonalPublishCell.h"
#import "WePersonalLeaveMessageCell.h"
#import "WePersonalLeaveMessageHeaderView.h"

static NSString *kWePersonalPublishCell_ID = @"kWePersonalPublishCell_ID";
static NSString *kWePersonalLeaveMessageCell_ID = @"kWePersonalLeaveMessageCell_ID";

@interface WePersonalController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) WePersonalHeaderInfoView *headerInfoView;
@property (nonatomic, strong) UIBarButtonItem *rightItem;
@property (nonatomic, strong) WePersonalTableHeaderView *tableHeaderView;
@property (nonatomic) WePersonalHeaderType datasourceType;

@end

@implementation WePersonalController

#pragma mark - 点击事件
- (void)clickActionWithType:(WePersonalInfoControlClickType)type {
    switch (type) {
        case WePersonalInfoControlClickTypeBePraised:
            kCX_LOG(@"点击了被赞数");
            break;
        case WePersonalInfoControlClickTypeFans:
            kCX_LOG(@"点击了粉丝数");
            break;
        case WePersonalInfoControlClickTypeFocusOn:
            kCX_LOG(@"点击了关注数");
            break;
    }
}

- (void)rightItemClickAction:(UIBarButtonItem *)item {
    kCX_LOG(@"点击了分享");
}

- (void)headerActionWithType:(WePersonalHeaderType)type {
    
    if (_datasourceType != type) {
        switch (type) {
            case WePersonalHeaderTypePublish:
                kCX_LOG(@"点击了发布");
                break;
            case WePersonalHeaderTypeEvaluation:
                kCX_LOG(@"点击了评价");
                break;
            default:
                break;
        }
        
        _datasourceType = type;
        
        [_myTableView reloadData];
    }
    
}

#pragma mark - tableview delegate & tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_datasourceType == WePersonalHeaderTypePublish) {
        WePersonalPublishCell *cell = [tableView dequeueReusableCellWithIdentifier:kWePersonalPublishCell_ID forIndexPath:indexPath];
        return cell;
    } else {
        WePersonalLeaveMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:kWePersonalLeaveMessageCell_ID forIndexPath:indexPath];
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_datasourceType == WePersonalHeaderTypePublish) {
        return 310;
    } else {
        return 73;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (_datasourceType == WePersonalHeaderTypePublish) {
        return nil;
    } else {
        WePersonalLeaveMessageHeaderView *headerView = [[WePersonalLeaveMessageHeaderView alloc] init];
        return headerView;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_datasourceType == WePersonalHeaderTypePublish) {
        return 0;
    } else {
        return 75;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

#pragma mark - 子视图
- (void)addSubiewsInView {
    
    self.navigationItem.rightBarButtonItem = self.rightItem;
    
    [self.view addSubview:self.headerInfoView];
    
    [_headerInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(140);
    }];
    
    [self.view addSubview:self.myTableView];
    
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerInfoView.mas_bottom).offset(20);
        make.left.right.bottom.equalTo(self.view);
    }];
}

- (WePersonalHeaderInfoView *)headerInfoView {
    if (!_headerInfoView) {
        _headerInfoView = [WePersonalHeaderInfoView new];
        
        __weak typeof(self) weakSelf = self;
        _headerInfoView.infoCtrlBlock = ^(WePersonalInfoControlClickType type) {
            [weakSelf clickActionWithType:type];
        };
        
        _headerInfoView.setIconBlock = ^{
            kCX_LOG(@"跳转设置头像页面");
        };
    }
    return _headerInfoView;
}

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.tableHeaderView = self.tableHeaderView;
        [_myTableView registerClass:[WePersonalPublishCell class] forCellReuseIdentifier:kWePersonalPublishCell_ID];
        [_myTableView registerClass:[WePersonalLeaveMessageCell class] forCellReuseIdentifier:kWePersonalLeaveMessageCell_ID];
    }
    return _myTableView;
}

- (WePersonalTableHeaderView *)tableHeaderView {
    if (!_tableHeaderView) {
        _tableHeaderView = [[WePersonalTableHeaderView alloc] init];
        _tableHeaderView.frame = (CGRect){0,0,kSCREEN_WIDTH,50};
        __weak typeof(self) weakSelf = self;
        _tableHeaderView.selectedBlock = ^(WePersonalHeaderType type) {
            [weakSelf headerActionWithType:type];
        };
    }
    return _tableHeaderView;
}



- (UIBarButtonItem *)rightItem {
    if (!_rightItem) {
        _rightItem = [[UIBarButtonItem alloc] initWithImage:kIMAGE(@"share") style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClickAction:)];
    }
    return _rightItem;
}

#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人";
    
    _datasourceType = WePersonalHeaderTypePublish;
    
    [self addSubiewsInView];
    
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
